var prefix = api + "/gen/tool/gen";

$(function() {
	var ds = datasourceList()
	renderStatisticsFilter('#data_source', ds)
	var options = {
		url: prefix + "/db/list",
		sortName: "createTime",
		sortOrder: "desc",
		showSearch: false,
		showRefresh: false,
		showToggle: false,
		showColumns: false,
		clickToSelect: true,
		rememberSelected: true,
		uniqueId: "tableName",
		columns: [{
				field: 'state',
				checkbox: true
			},
			{
				title: "序号",
				formatter: function(value, row, index) {
					return $.table.serialNumber(index);
				}
			},
			{
				field: 'tableName',
				title: '表名称',
				width: '20%',
				sortable: true
			},
			{
				field: 'tableComment',
				title: '表描述',
				width: '20%',
				sortable: true
			},
			{
				field: 'createTime',
				title: '创建时间',
				width: '20%',
				sortable: true
			},
			{
				field: 'updateTime',
				title: '更新时间',
				width: '20%',
				sortable: true
			}
		]
	};
	$.table.init(options);
});
function datasourceList() {
    var result = []
    $.ajax({
        url: api + '/gen/dsconf/list',
        method: 'get',
        async: false
    }).done(function (res) {
        result = res.data
    })
    return result
}
function renderStatisticsFilter(obj, ds) {
	ds.forEach(function(item) {
		$(obj).append('<option value="' + item.name + '">' + item.name + '</option>')
	})
}
/* 导入表结构-选择表结构-提交 */
function submitHandler() {
	var ds=$("#data_source").val();
	var rows = $.table.selectColumns("tableName");
	if (rows.length == 0) {
		$.modal.alertWarning("请至少选择一条记录");
		return;
	}
	var data = {
		"tables": rows.join(),
		"dsName":ds
	};
	$.operate.save(prefix + "/importTable", data);
}
